package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SkuInfo;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.service.SkuMangeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.tomcat.util.security.KeyStoreUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author terminal
 * @since 2023-06-09 10:39
 */
@RestController
@RequestMapping("/admin/product")
public class SkuMangeController {
    @Resource
    private SkuMangeService skuMangeService;

    /**
     * 根据spuId 获取spuImage 集合
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = skuMangeService.getSpuImageList(spuId);
        return Result.ok(list);
    }

    /**
     * 保存SkuInfo
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuMangeService.saveSkuInfo(skuInfo);
        return Result.ok();
    }



    /**
     * 分页查询商品SKU列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuByPage(@PathVariable("page") Long page,
                               @PathVariable("limit") Long limit,
                               @RequestParam("category3Id") Long category3Id) {
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        skuInfoPage = skuMangeService.getSkuByPage(skuInfoPage,category3Id);
        return Result.ok(skuInfoPage);
    }

    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuMangeService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuMangeService.cancelSale(skuId);
        return Result.ok();
    }
}
